<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar">
      <el-form :inline="true" :model="filters" :size="size">
        <el-row class="search-input">

          <el-form-item label="类型名称：">
            <el-input v-model="filters.title" clearable placeholder="类型搜索"></el-input>
          </el-form-item>
          <el-form-item label="创建时间：" prop="start">
            <el-date-picker
              v-model="filters.updateTimeStart"
              style="width:100%"
              type="datetime"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期"
            ></el-date-picker>
          </el-form-item>
        </el-row>
        <el-row class="search-button">
          <el-form-item>
            <kt-button
              icon="fa fa-search"
              :label="$t('action.search')"
              type="primary"
              @click="findPage(filters)"
            />
          </el-form-item>
          <el-form-item>
            <kt-button
              icon="fa fa-refresh" :label="$t('action.reset')"
              @click="reset"
              type="primary"
            />
          </el-form-item>
          <el-form-item>
            <kt-button
              icon="fa fa-hand-pointer-o"
              :label="$t('action.add')"
              @click="handleAdd"
              type="primary"
            />
          </el-form-item>
        </el-row>
        <el-form-item class="t_r w100">
          <el-button-group>
            <el-tooltip content="刷新" placement="top">
              <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
            </el-tooltip>
            <el-tooltip content="列显示" placement="top">
              <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
            </el-tooltip>
          </el-button-group>
        </el-form-item>
        <!--表格显示列界面-->
        <table-column-filter-dialog
          ref="tableColumnFilterDialog"
          :columns="columns"
          @handleFilterColumns="handleFilterColumns"
        ></table-column-filter-dialog>
      </el-form>
    </div>
    <!--表格内容栏-->
    <cy-table
      :height="350"
      :highlightCurrentRow="true"
      :content="pageResult"
      :totalSize="totalSize"
      :columns="filterColumns"
      :showSelection="false"
      :showBatchDelete="false"
      :tableHeaderList="tableHeaderList"
      @findPage="findPage"
      @handleEdit="handleEdit"
      @handleDelete="handleDelete"
      ref="CyTable"
    ></cy-table>
    <!--新增编辑界面-->
    <el-dialog
      :title="operation?'新增':'编辑'"
      width="40%"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataForm"
        label-width="100px"
        :rules="tableHeaderList"
        ref="dataForm"
        :size="size"
        label-position="right"
      >
        <el-row class="searchDialog-input">
          <el-form-item label="类型名称：" prop="name" :disabled="showname">
            <el-input v-model="dataForm.name" auto-complete="off" placeholder="请输入公众号名称"></el-input>
          </el-form-item>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :size="size" @click.native="dialogVisible = false">{{$t('action.cancel')}}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click.native="submitForm"
          :loading="editLoading"
        >{{$t('action.submit')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import PopupTreeInput from "@/components/PopupTreeInput";
  import CyTable from "@/views/Core/ProductTable";
  import KtButton from "@/views/Core/KtButton";
  import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
  import { format } from "@/utils/datetime";
  import { exportExcel } from "@/utils/excel";
  export default {
    components: {
      PopupTreeInput,
      CyTable,
      KtButton,
      TableColumnFilterDialog
    },
    data() {
      return {
        size: "small",
        filters: {},
        columns: [],
        filterColumns: [],
        showname: false,
        pageRequest: { pageNum: 1, pageSize: 10 },
        pageResult: [
          {
            name:"111",
            appid:"111",
            appsecret:"111",
            createTime:"111",
          }
        ],
        totalSize: 0,
        options: {},
        operation: false, // true:新增, false:编辑
        dialogVisible: false, // 新增编辑界面是否显示
        editLoading: false,


        // 新增编辑界面数据
        dataForm: {
          id: "",
          name: "",
          appid: "",
          appsecret: ""
        }
      };
    },
    methods: {
      // // 获取分页数据
      // findPage: function(data) {
      //   this.filters.t = "WeChatPulicNumber";
      //   this.$refs.CyTable.findPage(this.filters);
      // },
      findPage: function() {
        let this_ = this;
        if (this_.filters == undefined || this_.filters == null) {
          this_.filters = {};
        }
        this_.filters.start = this.pageRequest.pageNum;
        this_.filters.limit = this.pageRequest.pageSize;
        this.utils.request.queryWxPublicNum(this_.filters, function(res) {
          if (res.data.rows == null) {
            res.data.rows = [];
          }
          this_.pageResult = res.data.rows;
          this_.totalSize = Number(res.data.total);
        });
      },
      // 批量删除
      handleDelete: function(data) {
        console.log(data, "////");
        let param = {};
        param.id = data.params[0].id;
        this.utils.request.deleteWxPublicNum(param, data.callback);
      },
      // 显示新增界面
      handleAdd: function() {
        this.dialogVisible = true;
        this.operation = true;
        this.dataForm = {};
      },
      //列表下载
      downloadExcel() {
        this.$confirm("确定下载列表文件?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            let filename = "微信公众号配置";
            this.$refs.CyTable.exportData(
              this.filters,
              this.filterColumns,
              filename
            );
          })
          .catch(() => {});
      },
      // 显示编辑界面
      handleEdit: function(params) {
        this.showname = true;
        console.log(params);
        this.dialogVisible = true;
        this.operation = false;
        this.dataForm = Object.assign({}, params.row);
      },
      // 编辑
      submitForm: function() {
        this.$refs.dataForm.validate(valid => {
          if (valid) {
            this.editLoading = true;
            let params = Object.assign({}, this.dataForm);
            params.token = localStorage.getItem("token");
            this.utils.request.editWxPublicNum(params, this.editInfoBack);
          }
        });
      },

      // 新增修改回调
      editInfoBack: function(data) {
        if (data.code == "0000") {
          this.$message({ message: "操作成功", type: "success" });
          this.dialogVisible = false;
          this.$refs["dataForm"].resetFields();
        } else {
          this.$message({ message: "操作失败, " + data.msg, type: "error" });
        }
        this.findPage();
        this.dialogVisible = false;
        this.operation = false;
        this.editLoading = false;
      },
      // 时间格式化
      dateFormat: function(row, column, cellValue, index) {
        return format(row[column.property]);
      },
      // 处理表格列过滤显示
      displayFilterColumnsDialog: function() {
        this.$refs.tableColumnFilterDialog.setDialogVisible(true);
      },
      // 处理表格列过滤显示
      handleFilterColumns: function(data) {
        this.filterColumns = data.filterColumns;
        this.$refs.tableColumnFilterDialog.setDialogVisible(false);
      },
      // 处理表格列过滤显示
      initColumns: function() {
        this.columns = [
          { prop: "name", label: "序号", minWidth: 120 },
          { prop: "name", label: "类型名称", minWidth: 120 },
          { prop: "createTime", label: "创建时间", minWidth: 120 },
        ];
        var temp = [];
        $.each(this.columns, function(key, val) {
          temp.push(val);
        });
        this.filterColumns = temp;
      },
      resetForm(formName) {
        this.findPage();
      }
    },

    mounted() {
      this.initColumns();
    }
  };
</script>

<style lang='scss' scoped>
  .right{
    text-align: right !important;
  }
</style>
